package com.leet.code.thread;

import java.util.concurrent.Semaphore;

public class Code1225 {
    public static void main(String[] args) {

    }

    static class DiningPhilosophers {

        public DiningPhilosophers() {

        }

        Semaphore[] ss = {
                new Semaphore(1),
                new Semaphore(0),
                new Semaphore(0),
                new Semaphore(0),
                new Semaphore(0)
        };
        // call the run() method of any runnable to execute its code
        public void wantsToEat(int philosopher,
                               Runnable pickLeftFork,
                               Runnable pickRightFork,
                               Runnable eat,
                               Runnable putLeftFork,
                               Runnable putRightFork) throws InterruptedException {
            ss[philosopher].acquire();
            pickLeftFork.run();
            pickRightFork.run();
            eat.run();
            putLeftFork.run();
            putRightFork.run();
            ss[(philosopher + 1) % 5].release();
        }
    }

}
